/** 
	Final Update : 2012.05.16 
**/
Fsz.Component = (function ()
{
	var initialize = Fsz.Class({
		initialize : function ()
		{
			this.component        = [];
			this.component.length = 0;
		},

		loadJS : function (url, callback, charset)
		{
			
			var head   = document.getElementsByTagName('head')[0], 
				script = document.createElement('script'), 
				loaded = false;

			script.src     = url;
			script.type    = "text/javascript";
			script.charset = (!charset)? "utf-8" : charset;

			// for IE
			
			var msie = window.navigator.userAgent.indexOf("MSIE") > -1 ? true : false;
			
			script.onreadystatechange = function (){
				if (this.readyState == "loaded" || this.readyState == "complete")
				{
					if (loaded)
					{
						return;
					}
					loaded = true;
					callback();
				}
			};

			// for Modern Browser but IEs
			if(!msie){
				script.onload = function ()
				{
					callback();
				};
			}
			
			head.appendChild(script);
		},

		add : function (name, cls){
			this.component[name] = cls;
			// 추가된 Component의 수를 update시킨다.
			this.component.length++;
			
		},

		use : function (){
			var args     = Array.prototype.slice.call(arguments, 0), 
				callback = args[args.length - 1];

			// 마지막 인자 function을 제거
			if (typeof callback === "function")
			{
				args.pop();
			}

			var THIS = this, 
				len  = args.length,
				i;
			
			for (i = 0; i < len; i++){
				if (typeof this.component[args[i]] === "undefined"){
					var url = "../Fsz."+args[i]+"/fsz." + args[i] + ".js", 
						fn  = function (i){
							return function ()
							{
								THIS._notify(args, callback);
							};
						}(i);
					this.loadJS(url, fn);
				}
				
			}
		},

		_notify : function (args, callback){
			if (this.component.length == args.length){
				callback(this);
			}
		}
	});
	return new initialize();
})();